package com.yoomiito.app.share.ui;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;

import com.huawei.secure.android.common.util.LogsUtil;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.yoomiito.app.Constant;
import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.model.colleage.SubjectShareInfo;
import com.yoomiito.app.model.my.InviteFriend;
import com.yoomiito.app.share.presenter.InviteSharePresenter;
import com.yoomiito.app.ui.anyview.invite.InviteFriendView;
import com.yoomiito.app.ui.anyview.invite.ShareSubjectView;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.AllUtils;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.utils.ImageUtils;
import com.yoomiito.app.utils.LoginUtils;
import com.yoomiito.app.utils.wx.UMengShareUtils;
import com.yoomiito.app.widget.dialog.CustomDialog1;

import butterknife.BindView;
import butterknife.OnClick;
import kotlin.Unit;

/**
 * 相比旧版
 *    将分享部分拆分出来  适应不同的分享功能
 */
public class NewInviteShareActivity extends BaseActivity<InviteSharePresenter> {

    private CustomDialog1 mCustomDialog;
    @BindView(R.id.tv_center)
    TextView mTitleView;
    @BindView(R.id.frameLayout)
    FrameLayout mFrameLayout;

    private Bitmap mBitmap;
    private BaseShareView mBaseShareView;
    private int type;
    private long mId;
    @Override
    public int getLayoutId() {
        return R.layout.act_invite_share;
    }

    @Override
    public InviteSharePresenter newP() {
        return new InviteSharePresenter(App.mAppConfig);
    }

    public static void start(Context context, int type) {
        Intent intent = new Intent(context, NewInviteShareActivity.class);
        intent.putExtra(Constant.TYPE, type);
        context.startActivity(intent);
    }
    public static void start(Context context, int type,long id) {
        Intent intent = new Intent(context, NewInviteShareActivity.class);
        intent.putExtra(Constant.TYPE, type);
        intent.putExtra(Constant.ID,id);
        context.startActivity(intent);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        Intent intent = getIntent();
        type = intent.getIntExtra(Constant.TYPE, AllCode.InviteFriend);
        mId = intent.getLongExtra(Constant.ID,0);
        mTitleView.setVisibility(View.VISIBLE);
        switch (type) {
            case AllCode.InviteFriend:
                mTitleView.setText("邀请好友");
                mBaseShareView = new InviteFriendView(this, mFrameLayout);
                break;
            case AllCode.College:
                mTitleView.setText("课程分享");
                mBaseShareView = new ShareSubjectView(this, mFrameLayout);
                break;
        }

        mBaseShareView.setMakeBitmapListener(bitmap -> {
            mBitmap = bitmap;
            return Unit.INSTANCE;
        });
        requestData();

    }

    private void requestData() {
        showLoading();
        switch (type){
            case AllCode.InviteFriend:
                getP().getInviteADData();
                break;
            case AllCode.College:
                getP().getSubjectShareInfo(mId);
                break;
        }
    }

    @OnClick({R.id.iv_back_left, R.id.act_my_invite_friend_download, R.id.act_my_invite_friend_share_wx, R.id.act_my_invite_friend_share_wx_circle})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back_left:
                finish();
                break;
            case R.id.act_my_invite_friend_download:
                if (AllUtils.isFastClick()) {
                    return;
                }

                if (mBitmap == null) {
                    mBitmap = ImageUtils.getViewBitmap(mBaseShareView.mAdView);
                }

                if (Build.VERSION.SDK_INT >= 23) {
                    requestPermission();
                } else {
                    loadPic();
                }
                break;
            case R.id.act_my_invite_friend_share_wx:
                if (AllUtils.isFastClick()) {
                    return;
                }
                if (mBitmap == null) {
                    mBitmap = ImageUtils.getViewBitmap(mBaseShareView.mAdView);
                }
                UMengShareUtils.shareWXPic(SHARE_MEDIA.WEIXIN, this, mBitmap);
                break;
            case R.id.act_my_invite_friend_share_wx_circle:
                if (AllUtils.isFastClick()) {
                    return;
                }
                if (mBitmap == null) {
                    mBitmap = ImageUtils.getViewBitmap(mBaseShareView.mAdView);
                }
                UMengShareUtils.shareWXPic(SHARE_MEDIA.WEIXIN_CIRCLE, this, mBitmap);
                break;
        }
    }

    public void setInviteFriendData(InviteFriend inviteFriend) {
        mBaseShareView.setData(inviteFriend);
        dismissLoading();
    }

    public void setSubjectShareInfo(SubjectShareInfo info){
        mBaseShareView.setData(info);
        dismissLoading();

    }

    public void requestPermission() {
        final String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
        StringBuilder sb = null;
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                if (sb == null) {
                    sb = new StringBuilder();
                }
                String str = AllUtils.permissionTipMap(permission);
                if (sb.indexOf(str) == -1) {
                    if (!TextUtils.isEmpty(sb)) {
                        sb.append("\n\n");
                    }
                    sb.append(str);
                }
            }
        }
        if (sb == null || TextUtils.isEmpty(sb)) {
            AllUtils.requestPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE)
                    .subscribe(aBoolean -> {
                        if (aBoolean) loadPic();
                    });
        } else {
            if (mCustomDialog == null) {
                mCustomDialog = new CustomDialog1(this, () -> {
                    AllUtils.requestPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                    Manifest.permission.READ_EXTERNAL_STORAGE)
                            .subscribe(aBoolean -> {
                                if (aBoolean) loadPic();
                            });
                });
            }
            mCustomDialog.setContent(String.valueOf(sb));
            mCustomDialog.show();
        }


    }
    private void loadPic() {
        ImageLoader.getInstance().downloadPic(this, mBitmap);
        mBitmap = ImageUtils.createBitmapByView(this,mBaseShareView.mAdView);
        ImageUtils.saveBitmapToAlbum(this,mBitmap);
        LogsUtil.e("==================",mBitmap+"=====");

        Toast.makeText(context, "保存成功", Toast.LENGTH_SHORT).show();
    }

}
